﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IISLogAnalyzer
{
    public partial class UcPageManager : UserControl
    {
        private string _search = string.Empty;

        public UcPageManager()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 模糊查询
        /// </summary>
        public string uc_search { get; set; } = "";

        /// <summary>
        /// 页的索引
        /// </summary>
        public int uc_pageIndex
        {
            get { return int.Parse(ControlProxyHelper.GetValue(lbPageIndex, "Text").ToString() == string.Empty ? "1" : ControlProxyHelper.GetValue(lbPageIndex, "Text").ToString()); }
            set { ControlProxyHelper.SetValue(lbPageIndex, "Text", value.ToString()); }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        public int uc_pageSize
        {
            get { return int.Parse(ControlProxyHelper.GetValue(txtView, "Text").ToString() == string.Empty ? "50" : ControlProxyHelper.GetValue(txtView, "Text").ToString()); }
            set { ControlProxyHelper.SetValue(txtView, "Text", value.ToString()); }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int uc_pageCount
        {
            get { return int.Parse(ControlProxyHelper.GetValue(lbPageCount, "Text").ToString() == string.Empty ? "1" : ControlProxyHelper.GetValue(lbPageCount, "Text").ToString()); }
            set { ControlProxyHelper.SetValue(lbPageCount, "Text", value.ToString()); }
        }

        /// <summary>
        /// 设置页数描述 输入数据条数
        /// </summary>
        public int uc_setPageDescription
        {
            set
            {
                int recordCount = value;
                int pageCount = (recordCount <= uc_pageSize) ? 1 : (recordCount % uc_pageSize) > 0 ? (recordCount / uc_pageSize + 1) : (recordCount / uc_pageSize);

                ControlProxyHelper.SetValue(lbTotul, "Text", "当前第 " + ControlProxyHelper.GetValue(lbPageIndex, "Text").ToString() + " 页 / 共 " + pageCount + " 页");
                ControlProxyHelper.SetValue(lbCount, "Text", "共 " + recordCount + " 条");
                ControlProxyHelper.SetValue(lbPageCount, "Text", pageCount.ToString());

                if (pageCount == 1)
                {
                    ControlProxyHelper.SetValue(lbNex, "ForeColor", Color.Gray);
                    ControlProxyHelper.SetValue(lbPre, "ForeColor", Color.Gray);
                }
                else
                {
                    if (ControlProxyHelper.GetValue(lbPageIndex, "Text").ToString() == "1")
                    {
                        ControlProxyHelper.SetValue(lbPre, "ForeColor", Color.Gray);
                        ControlProxyHelper.SetValue(lbNex, "ForeColor", Color.Blue);
                    }
                    else if (ControlProxyHelper.GetValue(lbPageIndex, "Text").ToString() == pageCount.ToString())
                    {
                        ControlProxyHelper.SetValue(lbPre, "ForeColor", Color.Blue);
                        ControlProxyHelper.SetValue(lbNex, "ForeColor", Color.Gray);
                    }
                    else
                    {
                        ControlProxyHelper.SetValue(lbPre, "ForeColor", Color.Blue);
                        ControlProxyHelper.SetValue(lbNex, "ForeColor", Color.Blue);
                    }
                }
            }
        }



        private void lbPre_Click(object sender, EventArgs e)
        {
            if (lbPageIndex.Text == "1")
            {
                MessageBox.Show("当前是 首页 ！", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            lbPageIndex.Text = (int.Parse(lbPageIndex.Text) - 1).ToString();

            txtJump.Text = string.Empty;

            bindData(this.ParentForm.Name);
        }

        private void lbNex_Click(object sender, EventArgs e)
        {
            if (lbPageIndex.Text == lbPageCount.Text)
            {
                MessageBox.Show("当前是 尾页 ！", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            lbPageIndex.Text = (int.Parse(lbPageIndex.Text) + 1).ToString();

            txtJump.Text = string.Empty;

            bindData(this.ParentForm.Name);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            lbPageIndex.Text = "1";
            txtJump.Text = string.Empty;

            bindData(this.ParentForm.Name, 1);
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            uc_search = txtSearch.Text;
        }

        private void bindData(string formName, int typeOfSender = 0)
        {
            switch (formName)
            {
                case "FormIISLogAnalyzer":
                    ((FormIISLogAnalyzer)this.ParentForm).BindDate();
                    break;

            }
        }

        private void txtJump_TextChanged(object sender, EventArgs e)
        {
            if (ControlProxyHelper.GetValue(txtJump, "Text").ToString().Trim() == "")
            {
                return;
            }

            if (Utils.checkTextBox(txtJump, 1))
            {
                int j = Utils.ObjToInt(ControlProxyHelper.GetValue(txtJump, "Text"), 1);
                int p = Utils.ObjToInt(ControlProxyHelper.GetValue(lbPageCount, "Text"), 1);
                int i = Utils.ObjToInt(ControlProxyHelper.GetValue(lbPageIndex, "Text"), 1);

                if (j >= p)
                {
                    ControlProxyHelper.SetValue(txtJump, "Text", p.ToString());
                    j = p;
                }
                else if (j <= 0)
                {
                    ControlProxyHelper.SetValue(txtJump, "Text", i.ToString());
                    j = i;
                }

                ControlProxyHelper.SetValue(lbPageIndex, "Text", j.ToString());

                bindData(this.ParentForm.Name);
            }
        }


    }
}
